/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.senaclem.persistencia;


import co.edu.senaclem.modelo.Personal;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;

/**
 *
 * @author FELIPE
 */

@Stateless
public class PersonalDAO extends Persistencia implements IPersonalDAO{
  
    public static ResultSet resSet=null;
        
    @Override
    public void eliminar(Personal personal) throws Exception{
        try {
            if (conectar()) {
                String consulta = "delete from personal where documentopersonal='" +personal.getDocumento()+"'";
                areadb = con.createStatement();
                areadb.execute(consulta);

                desconectar();
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void modificar(Personal personal) throws Exception{
        try {
            if (conectar()) {
                String consulta = "update personal set nombrepersonal='" + personal.getNombre()+ "',apellidopersonal='"+personal.getApellido()+"',correopersonal='"+personal.getCorreo()+"',telefonopersonal='"+personal.getTelefono()+"' where documentopersonal='"+personal.getDocumento()+"'";
                //System.out.println("UPdate de Aprendiz "+consulta);
                areadb = con.createStatement();
                areadb.execute(consulta);

                desconectar();
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }   

    @Override
    public List<Personal> consultar() throws Exception {
        try {
            List<Personal> personales=new ArrayList<Personal>();
            if(conectar())
            {
                String consulta="Select * From personal";
                areadb= con.createStatement();
                resSet = areadb.executeQuery(consulta);                            
                
                while(resSet.next())
                {
                    Personal miPersonal=new Personal();
                    miPersonal.setDocumento(resSet.getLong("documentopersonal"));
                    miPersonal.setNombre(resSet.getString("nombrepersonal"));
                    miPersonal.setApellido(resSet.getString("apellidopersonal"));
                    miPersonal.setTelefono(resSet.getString("telefonopersonal"));
                    miPersonal.setCorreo(resSet.getString("correopersonal"));
                    miPersonal.setClave(resSet.getString("clavepersonal"));
                    
                    personales.add(miPersonal);                    
                }
                
                resSet.close();
                desconectar();
           }
            
           return personales;
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void insertar(Personal personal) throws Exception{
        try {
            if(conectar())
            {
                String consulta="insert into personal values('"+personal.getDocumento()+"','"+personal.getNombre()+"','"+personal.getApellido()+"','"+personal.getCorreo()+"','"+personal.getTelefono()+"',md5('"+personal.getClave()+"'))";
                areadb= con.createStatement();
                areadb.execute(consulta); 
                
                desconectar();
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public Personal consultarPorId(Long documento) throws Exception{
        try {
            Personal miPersonal=null;
            if(conectar())
            {
                String consulta="Select * From personal where documentopersonal='"+documento+"'";
                areadb= con.createStatement();
                resSet = areadb.executeQuery(consulta);                            
                
                if(resSet.next())
                {
                    miPersonal=new Personal();
                    miPersonal.setDocumento(resSet.getLong("documentopersonal"));
                    miPersonal.setNombre(resSet.getString("nombrepersonal"));
                    miPersonal.setApellido(resSet.getString("apellidopersonal"));
                    miPersonal.setTelefono(resSet.getString("telefonopersonal"));
                    miPersonal.setCorreo(resSet.getString("correopersonal"));
                    miPersonal.setClave(resSet.getString("clavepersonal"));  
                }
                
                resSet.close();
                desconectar();
            }
            
            return miPersonal;
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void cambiarContraseña(Personal personal) throws Exception {
        try {
            if (conectar()) {
                String consulta = "update personal set clavepersonal=md5('" + personal.getClave()+ "') where documentopersonal='"+personal.getDocumento()+"'";
                areadb = con.createStatement();
                areadb.execute(consulta);

                desconectar();
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }
    
}
